#!/bin/bash
clear

TEXT='\033[30;107;2m'
INFO='\033[97;44m'
ERROR='\033[97;41m'
SUCCESS='\033[97;42m'
GREEN='\033[92m'
YELLOW='\033[93m'
RESET='\033[0m'

if [ "${EUID}" -ne 0 ]; then
  echo -e "${ERROR} RALAT ${RESET}${TEXT} Anda tiada kebenaran untuk menjalankan skrip ini! ${RESET}"
  exit 1
fi

alamat_ip=$(wget -qO- ipv4.icanhazip.com)
alamat_hos=$(cat /etc/environment | grep 'DOMAIN' | cut -d '=' -f 2 | head -n 1)

function info_user {
  if add_user; then
    echo
    echo -e "${TEXT} ========================================================== ${RESET}"
    echo -e "${TEXT} DROPBEAR & OPENVPN - AKAUN PERCUBAAN                       ${RESET}"
    echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
    echo
    echo -e "${YELLOW}     Alamat IP:${PLAIN} ${GREEN}$alamat_ip${PLAIN}"
    echo -e "${YELLOW}   Nama domain:${PLAIN} ${GREEN}$alamat_hos${PLAIN}"
    echo -e "${YELLOW} Nama pengguna:${PLAIN} ${GREEN}$nama_pengguna${PLAIN}"
    echo -e "${YELLOW}   Kata laluan:${PLAIN} ${GREEN}$kata_laluan${PLAIN}"
    echo -e "${YELLOW}  Tempoh aktif:${PLAIN} ${GREEN}$tempoh_aktif hari${PLAIN}"
    echo -e "${YELLOW}  Tarikh luput:${PLAIN} ${GREEN}$tarikh_luput${PLAIN}"
    echo -e " ---------------------------------------------------------- "
    echo -e "${YELLOW}OpenVPN [TCP]:${RESET} ${GREEN} https://$alamat_hos/client-tcp.ovpn${RESET}"
    echo -e "${YELLOW}OpenVPN [UDP]:${RESET} ${GREEN} https://$alamat_hos/client-udp.ovpn${RESET}"
    echo -e "${YELLOW}OpenVPN [TLS]:${RESET} ${GREEN} https://$alamat_hos/client-tls.ovpn${RESET}"
    echo
    echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
    echo -e "${TEXT} Dicipta oleh Doctype, Dikuasakan oleh Cybertize.           ${RESET}"
    echo -e "${TEXT} ========================================================== ${RESET}"
    echo
  fi
}

function trial_user {
  nama_pengguna=$(tr </dev/urandom -dc A-Z | head -c5)
  egrep "^$nama_pengguna" /etc/passwd >/dev/null
  if [ $? -eq 0 ]; then
    echo -e "${ERROR} RALAT ${RESET}${TEXT} Nama pengguna sudah wujud! ${RESET}"
    exit 1
  fi
  kata_laluan=$(tr </dev/urandom -dc 0-9 | head -c5)
  tempoh_aktif=1
  tarikh_luput=$(date -d "$tempoh_aktif days" +"%F")

  useradd $nama_pengguna
  usermod -s /bin/false $nama_pengguna
  usermod -e $tarikh_luput $nama_pengguna
  echo -e "$kata_laluan\n$kata_laluan" | passwd $nama_pengguna &>/dev/null
}

function add_user {
  read -p "Masukkan nampa pengguna: " _namapengguna
  egrep "^$_namapengguna" /etc/passwd >/dev/null
  if [ $? -eq 0 ]; then
    echo -e "${ERROR} RALAT ${RESET}${TEXT} Nama pengguna sudah wujud! ${RESET}"
    exit 1
  fi
  read -p "Masukkan kata laluan: " _katalaluan
  read -p "Masukkan Tempoh aktif [Hari]: " _tempohaktif
  tarikh_luput=$(date -d "$_tempohaktif days" +"%F")

  useradd $_namapengguna
  usermod -s /bin/false $_namapengguna
  usermod -e $tarikh_luput $_namapengguna
  echo -e "$_katalaluan\n$_katalaluan" | passwd $_namapengguna &>/dev/null

  info_user
}

function renew_user {
  read -p "Masukkan nama pengguna: " _namapengguna
  egrep "^$_namapengguna" /etc/passwd >/dev/null
  if [ $? -ne 0 ]; then
    echo -e "${ERROR} RALAT ${RESET}${TEXT} Nama pengguna tidak wujud! ${RESET}"
    exit 1
  fi
  read -p "Masukkan tempoh aktif [Hari]: " _tempohaktif
  tarikh_luput=$(date -d "$_tempohaktif days" +"%F")

  passwd -u $_namapengguna
  usermod -e $tarikh_luput $_namapengguna

  clear
  echo
  echo -e "${TEXT} ========================================================== ${RESET}"
  echo -e "${TEXT} DROPBEAR & OPENVPN - PERBAHARUI AKAUN PENGGUNA             ${RESET}"
  echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
  echo
  echo -e "${YELLOW} Nama pengguna:${RESET} ${GREEN}$_namapengguna${RESET}"
  echo -e "${YELLOW}  Tempoh aktif:${RESET} ${GREEN}$_tempohaktif days${RESET}"
  echo -e "${YELLOW}  Tarikh luput:${RESET} ${GREEN}$tarikh_luput${RESET}"
  echo
  echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
  echo -e "${TEXT} Dicipta oleh Doctype, Dikuasakan oleh Cybertize.           ${RESET}"
  echo -e "${TEXT} ========================================================== ${RESET}"
  echo
}

function login_user {
  echo
  echo -e "${TEXT} DROPBEAR - LOG MASUK KLIEN                                 ${RESET}"
  echo
  cat "/var/log/auth.log" | grep -i dropbear | grep -i "Password auth succeeded" >/tmp/dropbear_login.txt
  getServiceID=($(ps aux | grep -i dropbear | awk '{print $2}'))
  for dpi in "${getServiceID[@]}"; do
    cat /tmp/dropbear_login.txt | grep "dropbear\[$dpi\]" >/tmp/dropbear_login_pid.txt
    getUserName=$(cat /tmp/dropbear_login_pid.txt | awk '{print $10}')
    getUserAddr=$(cat /tmp/dropbear_login_pid.txt | awk '{print $12}')
    totalClient=$(cat /tmp/dropbear_login_pid.txt | wc -l)
    [[ $totalClient -eq 1 ]] && echo "$dpi - $getUserName - $getUserAddr"
  done
  echo
  echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
  echo -e "${TEXT} Created by Doctype, Powered by Cybertize.                  ${RESET}"
  echo -e "${TEXT} Copyright 2021, Allright Reserved.                         ${RESET}"
  echo -e "${TEXT} ========================================================== ${RESET}"
  echo

  echo
  echo -e "${TEXT} OPENVPN - LOG MASUK KLIEN                                  ${RESET}"
  echo
  if [ -f "/var/log/openvpn/openvpn-auth.log" ]; then
    line=$(cat /var/log/openvpn/openvpn-auth.log | wc -l)
    a=$((3 + ((line - 8) / 2))) && b=$(((line - 8) / 2))
    cat /var/log/openvpn/ovpn-auth.log | head -n $a | tail -n $b | cut -d "," -f 1,2,5 | sed -e 's/,/   /g' >/tmp/vpn-login-db.txt
    cat /tmp/vpn-login-db.txt
  fi
  echo
  echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
  echo -e "${TEXT} Dicipta oleh Doctype, Dikuasakan oleh Cybertize.           ${RESET}"
  echo -e "${TEXT} ========================================================== ${RESET}"
  echo
}

function lists_user {
  echo
  echo -e "${TEXT} ========================================================== ${RESET}"
  echo -e "${TEXT} DROPBEAR & OPENVPN - SENARAI AKAUN PENGGUNA                ${RESET}"
  echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
  echo
  while read file; do
    senarai_klien="$(echo $file | cut -d ":" -f 1)"
    tarikh_luput="$(chage -l $senarai_klien | grep "Account expires" | awk -F": " '{print $2}')"
    user_uid="$(echo $file | grep -v nobody | cut -d ":" -f 3)"
    if [[ $user_uid -ge 1000 ]]; then
      printf "%-15s %2s\n" "$senarai_klien" "$tarikh_luput"
    fi
  done </etc/passwd
  jumlah_klien="$(awk -F ":" '$3 >= 1000 && $1 != "nobody" {print $1}' /etc/passwd | wc -l)"

  echo
  echo -e "${SUCCESS} JUMLAH ${RESET}${TEXT} $jumlah_klien pengguna ${RESET}"
  echo
  echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
  echo -e "${TEXT} Dicipta oleh Doctype, Dikuasakan oleh Cybertize.           ${RESET}"
  echo -e "${TEXT} ========================================================== ${RESET}"
  echo
}

function lock_user {
  read -p "Masukkan nama pengguna: " _namapengguna
  egrep "^$_namapengguna" /etc/passwd >/dev/null
  if [ $? -ne 0 ]; then
    echo -e "${ERROR} RALAT ${RESET}${TEXT} Nama pengguna tidak wujud! ${RESET}"
    exit 1
  fi
  passwd -l $_namapengguna

  clear
  echo
  echo -e "${TEXT} ========================================================== ${RESET}"
  echo -e "${TEXT} DROPBEAR & OPENVPN - KUNCI AKAUN                           ${RESET}"
  echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
  echo
  echo -e "${SUCCESS} INFO ${RESET}${TEXT} Berjaya mengunci kata laluan pengguna! ${RESET}"
  echo
  echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
  echo -e "${TEXT} Dicipta oleh Doctype, Dikuasakan oleh Cybertize.           ${RESET}"
  echo -e "${TEXT} ========================================================== ${RESET}"
  echo
}

function unlock_user {
  read -p "Masukkan nama pengguna: " _namapengguna
  egrep "^$_namapengguna" /etc/passwd &>/dev/null
  if [ $? -ne 0 ]; then
    echo -e "${ERROR} RALAT ${RESET}${TEXT} Nama pengguna tidak wujud! ${RESET}"
    exit 1
  fi
  passwd -u $_namapengguna

  clear
  echo
  echo -e "${TEXT} ========================================================== ${RESET}"
  echo -e "${TEXT} DROPBEAR & OPENVPN - BUKA KUNCI AKAUN                      ${RESET}"
  echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
  echo
  echo -e "${SUCCESS} INFO ${RESET}${TEXT} Berjaya buka kunci kata laluan pengguna ${RESET}"
  echo
  echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
  echo -e "${TEXT} Dicipta oleh Doctype, Dikuasakan oleh Cybertize.           ${RESET}"
  echo -e "${TEXT} ========================================================== ${RESET}"
  echo
}

function delete_user {
  read -p "Masukkan nama pengguna: " _namapengguna
  egrep "^$_namapengguna" /etc/passwd >/dev/null
  if [ $? -ne 0 ]; then
    echo -e "${ERROR} RALAT ${RESET}${TEXT} Nama pengguna tidak wujud! ${RESET}"
    exit 1
  fi
  passwd -l $_namapengguna
  userdel -f $_namapengguna

  clear
  echo
  echo -e "${TEXT} ========================================================== ${RESET}"
  echo -e "${TEXT} DROPBEAR & OPENVPN - PADAM AKAUN PENGGUNA                  ${RESET}"
  echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
  echo
  echo -e "${SUCCESS} INFO ${RESET}${TEXT} Berjaya memadamkan akaun pengguna ${RESET}"
  echo
  echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
  echo -e "${TEXT} Dicipta oleh Doctype, Dikuasakan oleh Cybertize.           ${RESET}"
  echo -e "${TEXT} ========================================================== ${RESET}"
  echo
}

echo
echo -e "${TEXT} DROPBEAR & OPENVPN                                         ${RESET}"
echo
echo -e "[01] ${GREEN}trial${RESET}          - ${YELLOW}Buat akaun percubaan${RESET}"
echo -e "[02] ${GREEN}create${RESET}         - ${YELLOW}Tambah akaun pengguna${RESET}"
echo -e "[03] ${GREEN}renew${RESET}          - ${YELLOW}Perbaharui akaun pengguna${RESET}"
echo -e "[04] ${GREEN}login${RESET}          - ${YELLOW}Senarai pengguna log masuk${RESET}"
echo -e "[05] ${GREEN}lists${RESET}          - ${YELLOW}Senaraikan akaun pengguna${RESET}"
echo -e "[06] ${GREEN}lock${RESET}           - ${YELLOW}Kunci/Lumpuhkan akaun pengguna${RESET}"
echo -e "[07] ${GREEN}unlock${RESET}         - ${YELLOW}Buka kunci/Dayakan akaun pengguna${RESET}"
echo -e "[08] ${GREEN}delete${RESET}         - ${YELLOW}Padam akaun pengguna${RESET}"
echo
echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
echo -e "${TEXT} Dicipta oleh Doctype, Dikuasakan oleh Cybertize.           ${RESET}"
echo -e "${TEXT} ========================================================== ${RESET}"
echo

read -p "Masukkan pilihan anda atau taip 'quit' untuk keluar: " PilihanMenu
case $PilihanMenu in
01 | trial) trial_user ;;
02 | create) add_user ;;
03 | renew) renew_user ;;
04 | login) login_user ;;
05 | lists) lists_user ;;
06 | lock) lock_user ;;
07 | unlock) unlock_user ;;
08 | delete) delete_user ;;
00 | quit) exit 0 ;;
*) echo -e "${ERROR} RALAT ${RESET}${TEXT} Pilihan tidak sah! ${RESET}" ;;
esac
